/*******************************************************************************
 * Copyright (c) 2013 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
/**
 * 
 */
package nz.co.senanque.pizzaorder.catalogue;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;

/**
 * @author Roger Parkinson
 *
 */
public class CatalogueFactory implements FactoryBean<HierarchicalContainer> {
	
	private Map<String, List<CatalogueItem>> m_map;
	private Map<Locale, HierarchicalContainer> m_cache = new HashMap<Locale, HierarchicalContainer>();
	@Autowired private MessageSource m_messageSource;

	public MessageSource getMessageSource() {
		return m_messageSource;
	}

	public void setMessageSource(MessageSource messageSource) {
		m_messageSource = messageSource;
	}

	public Map<String, List<CatalogueItem>> getMap() {
		return m_map;
	}

	public void setMap(Map<String, List<CatalogueItem>> map) {
		m_map = map;
	}

	@Override
	public HierarchicalContainer getObject() throws Exception {
		Locale locale = LocaleContextHolder.getLocale();
		HierarchicalContainer ret = m_cache.get(locale);
		if (ret != null)
		{
			return ret;
		}
		else
		{
			return makeCacheEntry(locale);
		}
	}
	
	private synchronized HierarchicalContainer makeCacheEntry(Locale locale)
	{
		HierarchicalContainer ret = m_cache.get(locale);
		if (ret != null)
		{
			return ret;
		}
		ret = new HierarchicalContainer();
		m_cache.put(locale,ret);
		for (Map.Entry<String, List<CatalogueItem>> entry: m_map.entrySet())
		{
			String parentItemId = getMessageSource().getMessage(entry.getKey(),null,entry.getKey(),locale);
			Item item = ret.addItem(parentItemId);
			ret.setChildrenAllowed(parentItemId, true);
			for (CatalogueItem catalogueItem: entry.getValue())
			{
//				String subItem = getMessageSource().getMessage(catalogueItem.toString(),null,catalogueItem.toString(),locale);
				Item item1 = ret.addItem(catalogueItem);
				ret.setParent(catalogueItem,parentItemId);
				ret.setChildrenAllowed(catalogueItem, false);
			}
		}
		return ret;
	}

	@Override
	public Class<? extends HierarchicalContainer> getObjectType() {
		return HierarchicalContainer.class;
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

}
